#region License, Terms and Conditions
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
#endregion
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using raya.Library.Helpers;
using raya.Models.messages;
using raya.Models.people;

namespace raya.Controllers
{
    public class messagesController : baseController
    {
        //
        // GET: /messages/
        public ActionResult Index()
        {
            if (Session["id"] == null)
            {
                return Redirect("/");
            }

            return View("Index");
        }

        public ActionResult inbox(string type)
        {
            return message_inbox("inbox");
        }

        public ActionResult sent(string type)
        {
            return message_inbox("sent");
        }

        public ActionResult message_inbox(string type)
        {
            int start = 0;
            const int count = 20;
            if (Request.QueryString["page"] != null &&
                PHPExt.is_numeric(Request.QueryString["page"]) &&
                int.Parse(Request.QueryString["page"]) > 0)
            {
                start = (int.Parse(Request.QueryString["page"]) - 1) * 20;
            }
            var messages = new messages();
            List<MessageRaya> msgs;
            if (type == "inbox")
            {
                msgs = messages.get_inbox(Session["id"].ToString(), start, count);
            }
            else if (type == "sent")
            {
                msgs = messages.get_sent(Session["id"].ToString(), start, count);
            }
            else
            {
                throw new Exception("invalid type");
            }
            var msgProfile = new MessagesProfile { messages = msgs, type = type };
            messages.Dispose();
            return View("messages", msgProfile);
        }

        public ContentResult notifications()
        {
            return Content("Not implemented, at some point this will container friend requests and requestShareApp notifications");
        }

        public void delete(string msgid)
        {
            var messages = new messages();
            var msg = messages.get_message(msgid);
            // silly special case if you send a message to your self
            if (msg.recipient == Session["id"].ToString() && msg.msg.senderId == Session["id"].ToString())
            {
                messages.delete_message(msgid, "to");
                messages.delete_message(msgid, "from");
                return;
            }
            string type;
            if (msg.recipient == Session["id"].ToString())
            {
                type = "to";
            }
            else if (msg.msg.senderId == Session["id"].ToString())
            {
                type = "from";
            }
            else
            {
                throw new Exception("This is not the message your looking for");
            }
            messages.Dispose();
            messages.delete_message(msgid, type);
        }

        public ActionResult get()
        {
            string messageId = Request.QueryString["messageId"] ?? "";
            string messageType = Request.QueryString["messageType"] != null && (Request.QueryString["messageType"] == "inbox" || Request.QueryString["messageType"] == "sent") ? Request.QueryString["messageType"] : "";
            if (messageId == "" || messageType == "")
            {
                throw new Exception("This is not the message you're looking for");
            }
            var messages = new messages();
            var msg = messages.get_message(messageId, messageType);
            if (msg.read != null && msg.read == "no")
            {
                messages.mark_read(messageId);
            }
            var msgProfile = new MessageProfile { message = msg, msgid = messageId, msgtype = messageType };
            messages.Dispose();
            return View("message", msgProfile);
        }

        public ActionResult compose()
        {
            var people = new people();
            var friends = people.GetFriends(Session["id"].ToString());
            people.Dispose();
            return View("compose", friends);
        }

        public void send()
        {
            string to = Request.Form["to"];
            string subject = Request.Form["subject"] != null ? PHPExt.strip_tags(Request.Form["subject"]).Trim() : null;
            string body = Request.Form["message"] != null ? PHPExt.strip_tags(Request.Form["message"]).Trim() : null;
            if (String.IsNullOrEmpty(to) || String.IsNullOrEmpty(subject))
            {
                throw new Exception("Uh what?");
            }
            var messages = new messages();
            messages.send_message(Session["id"].ToString(), to, subject, body);
            messages.Dispose();
        }
    }
}